package com.nursinghome.emp.controller;


import com.nursinghome.admin.service.MedicalStaffService;
import com.nursinghome.admin.service.UserService;
import com.nursinghome.bean.Reservation;
import com.nursinghome.bean.RespBean;
import com.nursinghome.bean.User;
import com.nursinghome.common.service.ReservationService;
import com.nursinghome.exception.NurisingHomeException;
import com.nursinghome.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/emp/reservation")
public class EmpReservationController {
    @Autowired
    private ReservationService reservationService;
    @Autowired
    private UserService userService;
    @Autowired
    private MedicalStaffService medicalStaffService;
    @GetMapping
    public RespBean selectAll(@RequestHeader(value = "token", required = false) String token,
                               Reservation reservation) {
        // 获取护工id
        User user = userService.selectById(Integer.parseInt(String.valueOf(JwtUtils.parseJwtToMap(token).get("id"))));
        String username = user.getUsername();
        Integer mid = medicalStaffService.selectByMusername(username);
        reservation.setMid(mid);
        reservation.setStatus(1);
        return RespBean.ok("",reservationService.selectAllByCondition(reservation));
    }

    //修改预约服务订单
    @PutMapping
    public RespBean update(@RequestBody Reservation reservation) throws NurisingHomeException {
        if (reservationService.updateByState(reservation)) {
            return RespBean.ok("修改成功");
        }
        return RespBean.error("修改失败");
    }
}
